/*set working directory*/
/*ssc install blindschemes*/

/*Table 4.1: Proportion Generalist, by INGO Population*/
clear
use "Data/missions_data.dta"
generate specialist=.
replace specialist=1 if progscopenumeric==1 | geoscopenumeric==1 | geoscopenumeric==2
replace specialist=0 if progscopenumeric==2 & geoscopenumeric==3
generate generalist=.
replace generalist=1 if specialist==0
replace generalist=0 if specialist==1
by field, sort: tab generalist 

/*Figure 4.2: Relationship between Programmatic and Geographic Scope*/
clear
use "Data/missions_data.dta"
graph bar (count), over(geographicscope) over(programmaticscope, ///
	label(labsize(medlarge))) ///
	ylabel(, angle(horizontal) labsize(medlarge)) ///
	ytitle("Number", size(medlarge)) scheme(plotplain) 
graph export "Figures/Figure4-2.pdf", as(pdf) replace

/*Figure 4.3: Differences between Generalist and Specialist INGOs in Age and Size*/
/*4.3a*/
clear
use "Data/missions_data.dta"
rename foundingyear year
keep if year>1992
generate specialist=.
replace specialist=1 if progscopenumeric==1 | geoscopenumeric==1 | geoscopenumeric==2
replace specialist=0 if progscopenumeric==2 & geoscopenumeric==3
generate generalist=.
replace generalist=1 if specialist==0
replace generalist=0 if specialist==1
kdensity year, nograph generate(x fx)
kdensity year if specialist==0, nograph generate(fx0) at(x)
kdensity year if specialist==1, nograph generate(fx1) at(x)
label var fx1 "Specialists"
label var fx0 "Generalists"
line fx0 fx1 x, sort ytitle(Density) xtitle("") scheme(plotplain) scale(1.5) lwidth(medthick) legend(off)
*Manually label generalists and specialists using Greyscale6 and Greyscale10
graph export "Figures/Figure4-3a.pdf", as(pdf) replace

/*4.3b*/
clear
use "Data/missions_data.dta"
rename foundingyear year
keep if year>1992
generate specialist=.
replace specialist=1 if progscopenumeric==1 | geoscopenumeric==1 | geoscopenumeric==2
replace specialist=0 if progscopenumeric==2 & geoscopenumeric==3
generate generalist=.
replace generalist=1 if specialist==0
replace generalist=0 if specialist==1
replace revenue=0 if revenue<0
generate logrevenue=log(revenue+1)
kdensity logrevenue, nograph generate(x fx)
kdensity logrevenue if specialist==0, nograph generate(fx0) at(x)
kdensity logrevenue if specialist==1, nograph generate(fx1) at(x)
label var fx1 "Specialists"
label var fx0 "Generalists"
line fx0 fx1 x, sort ytitle(Density) xtitle("") scheme(plotplain) scale(1.5) lwidth(medthick) legend(off)
*Manually label generalists and specialists using Greyscale6 and Greyscale10
graph export "Figures/Figure4-3b.pdf", as(pdf) replace

/*Figure 4.4: Total Generalist and Specialist American INGOs, 1992-2012*/
clear
use "Data/missions_data.dta"
drop if foundingyear>2012
rename foundingyear year
generate specialist=.
replace specialist=1 if progscopenumeric==1 | geoscopenumeric==1 | geoscopenumeric==2
replace specialist=0 if progscopenumeric==2 & geoscopenumeric==3
generate generalist=.
replace generalist=1 if specialist==0
replace generalist=0 if specialist==1
collapse (sum) generalist specialist, by(year) 
sort year
gen cum_generalist=generalist[1]
replace cum_generalist=generalist[_n]+ cum_generalist[_n-1] if _n>1
label variable cum_generalist "Generalists"
sort year
gen cum_specialist=specialist[1]
replace cum_specialist=specialist[_n]+ cum_specialist[_n-1] if _n>1
label variable cum_specialist "Specialists"
drop if year<1992
label variable year "Year"
line cum_generalist cum_specialist year, lwidth(medthick medthick) lpattern(solid dash) xlabel(1990(5)2012) scale(1.5) legend(off) xtitle("") ytitle("Total Number") scheme(plotplain)
*Manually label generalists and specialists using Greyscale6 and Greyscale10
graph export "Figures/Figure4-4.pdf", as(pdf) replace

/*Figure 4.5: The Correlates of American INGO Specialization*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
collapse (mean) density, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace year=year+1 /*to lag density*/
drop if year>2019
drop _merge
sort field year
tempfile density
save `density'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
sort field fisyr totrev
by field fisyr: generate top_4 = _n > (_N - 4)
collapse (sum) totrev if top_4==1, by(field fisyr)
rename totrev top4rev
sort field fisyr
tempfile top4
save `top4'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
sort field fisyr totrev
collapse (sum) totrev, by(field fisyr)
bysort field: gen lag_totrev = totrev[_n-1]
generate pct_change = (totrev - lag_totrev) / lag_totrev * 100
generate abs_pct_change = abs(pct_change)
sort field fisyr
merge 1:1 field fisyr using `top4'
drop _merge
generate concentration4firm=top4rev/totrev
rename fisyr year
replace year=year+1 /*to lag concentration*/
sort field year
tempfile concentration
save `concentration'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
egen rank = rank(totrev), by(field fisyr)
egen count = count(totrev), by(field fisyr)
gen pct = 100 * (rank - 0.5) / count
gen percentile99=0
replace percentile99=1 if rank==count
replace percentile99=1 if rank!=count & pct>=98.5
collapse (sum) totrev if percentile99==1, by(field fisyr)
rename totrev rev99percentile
rename fisyr year
replace year=year+1 /*to lag alternative measure of concentration*/
sort field year
tempfile 99percentile
save `99percentile'

use "Data/missions_data.dta", clear
drop if foundingyear>2012
rename foundingyear year
generate specialist=.
replace specialist=1 if progscopenumeric==1 | geoscopenumeric==1 | geoscopenumeric==2
replace specialist=0 if progscopenumeric==2 & geoscopenumeric==3
rename field fieldalt
generate field=.
replace field=2 if fieldalt=="Relief"
replace field=3 if fieldalt=="Democracy"
replace field=4 if fieldalt=="Conservation"
replace field=5 if fieldalt=="Health"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
merge m:1 field year using `density'
drop _merge
sort field year
merge m:1 field year using `concentration'
drop _merge
sort field year
merge m:1 field year using `99percentile'

generate top1percentile=rev99percentile/totrev

replace revenue=0 if revenue<0
generate logrevenue=log(revenue+1)
label variable logrevenue "INGO Revenue"

generate dc=0
replace dc=1 if state=="DC"
label variable dc "DC Headquarters"

label variable faithbased "Faith-based INGO"
label variable concentration4firm "Concentration"

egen stddensity=std(density)
label variable stddensity "Density"

generate Sept11=0
replace Sept11=1 if year==2002 | year==2003 | year==2004
label variable Sept11 "Post-9/11"

egen volatility=std(abs_pct_change)
label variable volatility "Resource Volatility"

reg specialist logrevenue dc faithbased if year>1992, vce(cluster field)
estimates store Baseline

reg specialist logrevenue dc faithbased stddensity concentration4firm Sept11 volatility if year>1992, vce(cluster field)
estimates store Full

coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)

graph export "Figures/Figure4-6.pdf", as(pdf) replace
